import { Component, Injector } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { UserService } from './../../providers/UserService';
import { PIC_URL } from './../../providers/Config';
import { Events } from 'ionic-angular';
import { StorageService } from '../../providers/StorageService';
declare var $: any;

/**
 * Generated class for the ShopallComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'shopall',
  templateUrl: 'shopall.html'
  
})
export class ShopallComponent  extends BasePage{

  sum:number;
  
  picrooturl = ''; //图片服务器的地址
  userinfo = {}//获取内存中的数据
  orderList1=[]   //获取到的全部订单   0
  flag:boolean;   //显示控制器   
  item=[];        //总数据
  pop=1;      //查询页数
  loading:boolean=false;    //加载全部
  bottom1:boolean=false;    //加载全部底部显示器
  
  constructor(injector: Injector,   public events: Events,public userService: UserService ,public stc:StorageService ) {
    super(injector);
    console.log('Hello ShopallComponent Component');
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    this.getlist1('');
    events.subscribe('user:created', (hello1, time) => {
      if(hello1==1){
        //全部有数据更新
        console.log("待服务有数据更新")
        this.again();
      }
    });
  }
  
  //获取全部店铺订单
   async getlist1(infiniteScroll){
    let param={shop_id:this.userinfo['shop_id'],type_id:0,pop:this.pop}   //默认6条
    console.log(param);
    let result=await this.userService.shopOrderAPI(param);
    if(result.code ==1){
        if(this.pop==1){
          this.sum=result.count;
        }
        this.pop++;
        this.orderList1 = this.orderList1.concat(result.data);
        if(this.orderList1.length==0){
          this.flag=false;
        }else{
          this.flag =true;
        }
        if(infiniteScroll){
          infiniteScroll.complete(); //请求完成数据更新页面
          if(result.data.length<6){
            $(".box ion-infinite-scroll-content").css("display","none");
            this.bottom1=true;
          }
        }
    }else{
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }
  
  doInfinite(infiniteScroll){
    this.bottom1 = false;
    this.getlist1(infiniteScroll);
  }

  //打开拒绝理由页面
  refuse(i){
    this.stc.removeItem("back2");
    let profileModal = this.modalCtrl.create("RefusePage",{item:i,par:0});
    profileModal.onDidDismiss(data => {
      if(data == "ok"){
        this.again();
        let hello=0;
        this.events.publish("user:created",hello,Date.now());
      }
    });
    profileModal.present();
  }

  //因为其他页面数据更新，所以这个也要重新初始化
  again(){
    this.pop=1;
    this.bottom1 = false;
    this.orderList1=[];
    $(".box ion-infinite-scroll-content").css("display","block");
    this.getlist1('');
  }
}
